package com.example.spring_project.service.impl;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spring_project.entity.Book;
import com.example.spring_project.mapper.BookMapper;
import com.example.spring_project.service.BookService;
import com.example.spring_project.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {
    private final BookMapper bookMapper;
    @Autowired
    public BookServiceImpl(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }
    @Override
    public Book getById(int id) {
        return bookMapper.getById(id);
    }

    @Override
    public synchronized Result<Object> updateById(int id, int stock) {
        if (queryStockById(id)>=0 && stock>=0 &&bookMapper.updateBookStockById(id,stock)==1){
            return new Result<>("200","ok",null);
        }else return new Result<>("500","更新失败",null);
    }

    @Override
    public int queryStockById(int id) {
        return bookMapper.queryStockById(id);
    }

    @Override
    public Boolean deleteById(int id) {
        return bookMapper.deleteBookById(id)==1;
    }
    

}
